import CITY from '../newcity.min';
import getIn from '../../../../yali-sdk/utils/getIn';
const gRegion = CITY.region;
Component({
    properties: {
        visible: {
            type: Boolean,
            value: false,
            observer(newVal, oldVal) {
                if (newVal) {
                    this.showModal();
                }
            },
        },
    },
    data: {
        provinces: {},
        provinceId: undefined,
        provinceValue: '',
        cities: {},
        cityId: undefined,
        cityValue: '',
        zones: {},
        zoneId: undefined,
        zoneValue: '',
        provinceIndex: 0,
        cityIndex: 0,
        zoneIndex: 0,
        animationData: {},
        index: [0, 0, 0],
    },
    attached() {
        const provinces = this.getArrayValue(gRegion);
        const provinceId = getIn(provinces, [0, 'id']);
        const provinceValue = getIn(provinces, [0, 'name']);
        const cities = getIn(provinces, [0, 'child']) && this.getArrayValue(getIn(provinces, [0, 'child']));
        const cityId = getIn(cities, [0, 'id']);
        const cityValue = getIn(cities, [0, 'name']);
        const zones = getIn(cities, [0, 'child']) && this.getArrayValue(getIn(cities, [0, 'child']));
        const zoneId = getIn(zones, [0, 'id']);
        const zoneValue = getIn(zones, [0, 'name']);
        const provinceIndex = 0;
        const cityIndex = 0;
        const zoneIndex = 0;
        this.setData({
            provinces, provinceId, provinceValue, cities, cityId, cityValue, zones, zoneId, zoneValue, provinceIndex, cityIndex, zoneIndex,
        });
    },
    methods: {
        showModal() {
            this.timers && clearTimeout(this.timers);
            const animation = wx.createAnimation({
                duration: 200,
                timingFunction: 'linear',
                delay: 0,
            });
            this.animation = animation;
            const window_height = getIn(getApp(), ['globalData', 'window_height']);
            animation.translateY(window_height).step();
            const _height = 0;
            this.setData({ animationData: animation.export(), visible: true });
            this.times = setTimeout(() => {
                animation.translateY(_height).step();
                this.setData({
                    animationData: animation.export(),
                });
            }, 200);
        },
        getArrayValue(values) {
            const arrayValues = []
            for (const key in values) {
                arrayValues.push(values[key]);
            }
            return arrayValues;
        },
        /** 取消弹出框 */
        hideModal() {
            this.timers && clearTimeout(this.timers);
            const animation = wx.createAnimation({
                duration: 200,
                timingFunction: 'linear',
                delay: 0,
            });
            this.animation = animation;
            const window_height = getIn(getApp(), ['globalData', 'window_height']);
            animation.translateY(window_height).step();
            this.setData({ animationData: animation.export() });
            this.times = setTimeout(() => {
                animation.translateY(0).step();
                this.setData({
                    animationData: animation.export(),
                    visible: false,
                });
            }, 200);
        },
        changePickerValue(e) {
            const indexArray = getIn(e, ['detail', 'value']);
            const provinceIndex = indexArray[0];
            const cityIndex = indexArray[1];
            const zoneIndex = indexArray[2];
            if (this.data.provinceIndex != provinceIndex) {
                this.changeProvince(provinceIndex);
            } else if (this.data.cityIndex != cityIndex) {
                this.changeCity(cityIndex);
            } else if (this.data.zoneIndex != zoneIndex) {
                this.changeZone(zoneIndex);
            }
        },
        changeProvince(provinceIndex) {
            const province = this.data.provinces[provinceIndex];
            const provinceId = getIn(province, ['id']);
            const provinceValue = getIn(province, ['name']);
            const cities = getIn(province, ['child']) && this.getArrayValue(getIn(province, ['child']));
            const cityId = getIn(cities, [0, 'id']);
            const cityValue = getIn(cities, [0, 'name']);
            const zones = getIn(cities, [0, 'child']) && this.getArrayValue(getIn(cities, [0, 'child']));
            const zoneId = getIn(zones, [0, 'id']);
            const zoneValue = getIn(zones, [0, 'name']);
            const index = [provinceIndex, 0, 0];
            this.setData({
                provinceId, provinceValue, cities, cityId, cityValue, zones, zoneId, zoneValue, provinceIndex, cityIndex: 0, zoneIndex: 0, index,
            });
        },
        changeCity(cityIndex) {
            const city = this.data.cities[cityIndex];
            const cityValue = getIn(city, ['name']);
            const cityId = getIn(city, ['id']);
            const zones = getIn(city, ['child']) && this.getArrayValue(getIn(city, ['child']));
            const zoneId = getIn(zones, [0, 'id']);
            const zoneValue = getIn(zones, [0, 'name']);
            const index = [this.data.provinceIndex, cityIndex, 0];
            this.setData({
                cityValue, cityId, zones, zoneId, zoneValue, cityIndex, zoneIndex: 0, index,
            });
        },
        changeZone(zoneIndex) {
            const zone = this.data.zones[zoneIndex];
            const zoneId = getIn(zone, ['id']);
            const zoneValue = getIn(zone, ['name']);
            const index = [this.data.provinceIndex, this.data.cityIndex, zoneIndex];
            this.setData({
                zoneId, zoneValue, zoneIndex, index,
            });
        },
        /** 保存地址 */
        saveAddress() {
            this.hideModal();
            const address = this.data.provinceValue + this.data.cityValue + this.data.zoneValue;
            this.triggerEvent('changeregin', {
                address, provinceId: this.data.provinceId, cityId: this.data.cityId, zoneId: this.data.zoneId,
            });
        },
    },

});
